//
//  HomeNoticeDetailViewController.m
//  CarManager
//
//  Created by 王森林 on 2019/1/24.
//  Copyright © 2019年 王森林. All rights reserved.
//

#import "HomeNoticeDetailViewController.h"

@interface HomeNoticeDetailViewController ()
@property (nonatomic, strong) NSDictionary *dataDict;
@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong) UIView *topView;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UILabel *numLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@end

@implementation HomeNoticeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self fetchDataFromNet];
    // Do any additional setup after loading the view.
}
- (void)createUI
{
    [self.view addSubview:self.scroll];
    self.scroll.frame = CGRectMake(0, 0, BOUNDWIDTH, BOUNDHEIGHT);
    
    
    [self.scroll addSubview:self.topView];
    [self createTopView];
    [self createImgsView];
}

- (void)createImgsView
{
    NSArray *imgsAry = [self.dataDict objectForKey:@"imgList"];
    if (imgsAry.count > 0) {
        __block float y = self.contentLabel.size.height+self.contentLabel.origin.y+10;
        
        [imgsAry enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString *url = APPEND_URL(BASE_URL, [obj objectForKey:@"TPLJ00"]);
            UIImageView *imageView = [[UIImageView alloc] init];
            [imageView sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                NSLog(@"大小%f",image.size.width);
                NSLog(@"大小%f",image.size.height);
                float width = BOUNDWIDTH - 30;
                if (image.size.width != 0) {
                    width = image.size.width;
                }
                float height = 100;
                if (image.size.height != 0) {
                    height = image.size.height;
                }
                
                float rate = (BOUNDWIDTH - 30)/width;
                imageView.frame = CGRectMake(15, y, BOUNDWIDTH- 30, rate*height);
            }];
            y = y + imageView.frame.size.height+15;
            [self.scroll addSubview:imageView];
        }];
        self.scroll.contentSize = CGSizeMake(BOUNDWIDTH, y+100);
    }
    
}

- (void)createTopView
{
    
    NSDictionary *infoDict = [self.dataDict objectForKey:@"info"];
    self.titleLabel.text = [infoDict objectForKey:@"BT0000"];
//    [self.imgView sd_setImageWithURL:[NSURL URLWithString:APPEND_URL(BASE_URL, [infoDict objectForKey:@"MDTP00"])]];
    self.imgView.image = IMG(@"notice1");
    self.subtitleLabel.text = @"平行先锋";
    self.dateLabel.text = [infoDict objectForKey:@"FBSJ00"];
//    self.numLabel.text = [NSString stringWithFormat:@"阅读:  %@",[infoDict objectForKey:@"LLS000"]];
    
    [self.topView addSubview:self.titleLabel];
    [self.topView addSubview:self.imgView];
    [self.topView addSubview:self.subtitleLabel];
    [self.topView addSubview:self.dateLabel];
    [self.topView addSubview:self.numLabel];
    
    CGSize size = [CommonMethod labWidthFromStr:self.titleLabel.text andFont:self.titleLabel.font andWidth:BOUNDWIDTH -30];
    self.titleLabel.frame = CGRectMake(15, 15, BOUNDWIDTH - 30, size.height + 10);
    self.imgView.frame = CGRectMake(15, size.height + 10+10+15, 50, 50);
    self.imgView.layer.cornerRadius = 25;
    self.imgView.layer.masksToBounds = YES;
    self.subtitleLabel.frame = CGRectMake(75,  size.height + 10+10+20, BOUNDWIDTH - 90, 15);
    self.dateLabel.frame = CGRectMake(75, size.height+85-20, 120, 14);
    self.numLabel.frame = CGRectMake(200, size.height+85-20, BOUNDWIDTH - 215, 15);
    self.topView.frame = CGRectMake(0, 0, BOUNDWIDTH, size.height+80);
    
    [self.scroll addSubview:self.contentLabel];
    
    NSString * htmlString = [infoDict objectForKey:@"WZNR00"];
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    
    self.contentLabel.attributedText = attrStr;
    self.contentLabel.frame = CGRectMake(15, self.topView.frame.size.height+10, BOUNDWIDTH - 30, size.height + 15);
    
    
}


- (void)fetchDataFromNet
{
    //收藏夹列表
    //    UserRes *user = [CommonMethod getUser];
    NSDictionary *params = @{
                             @"FKEY":[CommonMethod createMD5Key:@"ARTICLE_ID"],
                             @"ARTICLE_ID":self.ID
                             
                             };
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_2 params:params md5Paraname:nil success:^(id  _Nullable response) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        self.dataDict  = response;
        [self createUI];
        //        NSDictionary *resultDict  = response;
        //        self.listAry = [NSMutableArray arrayWithArray:[resultDict objectForKey:@"list"]];
        //        [self.mainTableView reloadData];
        
        
        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
        
    }];
    
    
    
}

- (UIScrollView *)scroll
{
    if (!_scroll) {
        _scroll = [UIScrollView new];
    }
    return _scroll;
    
}

- (UIView *)topView
{
    if (!_topView) {
        _topView = [UIView new];
    }
    return _topView;
    
}

-  (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.numberOfLines = 0;
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = B_FONT_SIZE(16);
    }
    return _titleLabel;
    
}

-  (UILabel *)subtitleLabel
{
    if (!_subtitleLabel) {
        _subtitleLabel = [UILabel new];
        _subtitleLabel.textColor = Common_TextColor;
        _subtitleLabel.font = FONT_SIZE(15);
    }
    return _subtitleLabel;
    
}

- (UIImageView *)imgView
{
    
    if (!_imgView) {
        _imgView = [UIImageView new];
    }
    return _imgView;
}

-  (UILabel *)dateLabel
{
    if (!_dateLabel) {
        _dateLabel = [UILabel new];
        _dateLabel.textColor = Common_TextLowColor;
        _dateLabel.font = FONT_SIZE(11);
    }
    return _dateLabel;
    
}

-  (UILabel *)numLabel
{
    if (!_numLabel) {
        _numLabel = [UILabel new];
        _numLabel.textColor = Common_TextLowColor;
        _numLabel.font = FONT_SIZE(11);
        _numLabel.textAlignment = NSTextAlignmentRight;
    }
    return _numLabel;
    
}


-  (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [UILabel new];
        _contentLabel.textColor = Common_HeadTitle_Color;
        _contentLabel.font = FONT_SIZE(15);
    }
    return _contentLabel;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
